package algdat;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Stack;

/**
 * @author Mikko Puustinen (mikko.puustinen@gmail.com)
 * @version 0.1, 2007-nov-22
 */
public class Train {
    private Stack<Integer> inTrack;
    private Stack<Integer> sideTrack;
    private Stack<Integer> outTrack;
    private Scanner reader = null;
    private boolean isPossible;

    public Train() throws FileNotFoundException {
        reader = new Scanner(new File("train.dat"));
        inTrack = new Stack<Integer>();
        sideTrack = new Stack<Integer>();
        outTrack = new Stack<Integer>();
        isPossible = false;
        reader.nextInt();
    }

    public static void main(String[] args) throws FileNotFoundException {
        new Train().start();
    }

    public void start() {
        final long start = System.nanoTime();
        while (reader.hasNext()) {
            final int wagons = reader.nextInt();
            final Stack<Integer> temp = new Stack<Integer>();
            for (int i = wagons; i > 0; i--) {
                inTrack.push(i);
                temp.push(reader.nextInt());
            }
            for (int i = temp.size(); i > 0; i--) {
                outTrack.push(temp.pop());
            }
            solve();
            System.out.println(isPossible ? "JA" : "NEJ");
            isPossible = false;
            inTrack.clear();
            outTrack.clear();
        }
        final long end = System.nanoTime();
        System.out.printf("Time: %.3f ms", ((float)end - start) / 1000000);
    }

    public void solve() {
        if (inTrack.size() == 0 && sideTrack.size() == 0) {
            isPossible = true;
        }

        if (inTrack.size() > 0) {
            final Integer temp = inTrack.pop();
            sideTrack.push(temp);
            solve();
            inTrack.push(sideTrack.pop());
        }

        if (sideTrack.size() > 0) {
            if (sideTrack.peek().equals(outTrack.peek())) {
                final Integer sideTemp = sideTrack.pop();
                final Integer outTemp = outTrack.pop();
                solve();
                sideTrack.push(sideTemp);
                outTrack.push(outTemp);
            }
        }
    }
}
